<template>
	<div class="container">
		<el-dialog
			title="绑定角色"
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			destroy-on-close
			v-model="isShowDialog"
			:show-close="false"
			width="320px"
		>
			<el-form :model="ruleForm" ref="userForm" :rules="rules" size="small" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="账号" prop="account">
							<el-input v-model="ruleForm.account" placeholder="请输入账号" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="昵称" prop="nickName">
							<el-input v-model="ruleForm.nickName" placeholder="请输入昵称" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="绑定角色" prop="roleId">
							<el-cascader
								style="width:100%"
								placeholder="请选择角色"
								v-model="ruleForm.roleId"
								:props="{
									value: 'id',
									label: 'roleName',
									checkStrictly: true
								}"
								:options="role_list"
								filterable
								clearable
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="small">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="small">提 交</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref, defineComponent } from 'vue';
import { ElMessageBox } from 'element-plus';
import { getList } from '/@/api/platform/role/index'
import { set_role } from '/@/api/platform/user/index'
export default defineComponent({
	name: 'SetRole',
	setup(props: any, context: any) {
		const state = reactive({
			isShowDialog: false,
			ruleForm: {
				id: 0,
				account: "",
				nickName: "",
				roleId: [],
			},
			rules: {
				roleId: [{
					required: true,
					validator: (rule: any, value: any, callback: any) => {
						if (value === null || value.length <= 0) {
							callback(new Error('请选择用户绑定的角色'))
						} else {
							callback()
						}
					},
					trigger: "blur"
				}]
			},
			role_list: []
		});
		const userForm = ref();
		const getRoles = () => {
			getList({}).then((res) => {
				state.role_list = res.data.rows;
			})
		};
		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
			getRoles();
			state.ruleForm.id = row?.id;
			state.ruleForm.account = row?.account;
			state.ruleForm.nickName = row?.nickName;
			state.ruleForm.roleId = [row?.roleId]
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
		};
		// 取消
		const onCancel = () => {
			closeDialog();
			initForm();
		};
		const initForm = () => {
			userForm.value.resetFields();
		};
		// 新增
		const onSubmit = () => {
			userForm.value.validate((valid: boolean) => {
				if (!valid) {
					return false;
				}
				state.ruleForm.roleId = state.ruleForm.roleId[0]
				set_role(state.ruleForm).then((res) => {
					ElMessageBox.alert(res.Message, '提示', {}).then(() => {
						// 刷新父级数据
						context.emit("sendRefresh");
						closeDialog(); // 关闭弹窗
						initForm();
					}).catch(() => { });
				})
			})
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
			userForm
		};
	},
});
</script>